package com.junyuan.gsp.web;

import com.junyuan.gsp.bean.Menu;
import com.junyuan.gsp.bean.Role;
import com.junyuan.gsp.bean.User;
import com.junyuan.gsp.service.MenuService;
import com.junyuan.gsp.service.RoleService;
import com.junyuan.gsp.service.UserService;
import com.junyuan.gsp.util.SubjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: maxwellens
 */
@Controller
public class PageController
{
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    @GetMapping(value = {"/index.html", "/"})
    public String index(Model model)
    {
        User user = SubjectUtil.getUser();
        model.addAttribute("user", user);
        if (user != null)
        {
            List<Menu> menus = menuService.findOwnedTreeMenus(user.getId());
            model.addAttribute("menus", menus);
        }
        return "index";
    }

    @GetMapping("/user.html")
    public String user(Model model)
    {
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        return "user";
    }

}
